/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enterprise.web;

import com.assg2.dao.DaoActor;
import com.assg2.dao.DaoFavouriteActor;
import com.assg2.dao.DaoFavouriteGenre;
import com.assg2.dao.DaoGenre;
import com.assg2.dao.DaoViewer;
import com.movie.Actor;
import com.movie.FavouriteActor;
import com.movie.FavouriteActorId;
import com.movie.FavouriteGenre;
import com.movie.FavouriteGenreId;
import com.movie.Genre;
import com.movie.Viewer;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Kevin
 */
public class EditViewerProfileCommand implements Command{
    public EditViewerProfileCommand(){
    }
    
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //TODO: set the response content type to "text/html" here
        //TODO: You'll have to use getWriter() to output some HTML texts
        //TODO: close the output stream

        //response.setContentType("text/html");// from response, set content type
        PrintWriter out = response.getWriter();// from response, get output writer

        HttpSession session = request.getSession(true);

        DaoViewer dv = new DaoViewer();
            Viewer vieweru = (Viewer)request.getSession().getAttribute("user");
            Viewer viewer = (Viewer)dv.findById(vieweru.getIdviewer());
            viewer.setPassword((String)request.getParameter("password"));
            viewer.setFirstname((String)request.getParameter("firstname"));
            viewer.setLastname((String)request.getParameter("lastname"));
            viewer.setNickname((String)request.getParameter("nickname"));
            viewer.setEmail((String)request.getParameter("email"));
            String dateString = request.getParameter("calendarField");
            
            Date birthday = new java.sql.Date(TransformToDate(dateString).getTime());
            
//            
//            Date birthday = new java.sql.Date(Integer.parseInt(request.getParameter("year"))-1900, Integer.parseInt(request.getParameter("month"))-1, Integer.parseInt(request.getParameter("day")));
            viewer.setYearofbirth(birthday);

            
            Set<Genre> genreSet = new HashSet<Genre>();
            //test
           // viewer.getGenres().clear();
            dv.update(viewer);
            //DaoFavouriteGenre dfg = new DaoFavouriteGenre();
            
           
            
            String[] genreStrings = request.getParameterValues("genres");
            for(int i = 0; i < genreStrings.length; i++){
                int genreId = Integer.parseInt(genreStrings[i]);
                DaoGenre dg = new DaoGenre();
                Genre genre = (Genre)dg.findById(genreId);
                FavouriteGenreId fgi = new FavouriteGenreId(viewer.getIdviewer(), genreId);
                FavouriteGenre fg = new FavouriteGenre(fgi);
                viewer.getGenres().add(genre);
                genre.getViewers().add(viewer);
                DaoFavouriteGenre dfg = new DaoFavouriteGenre();
                dfg.save(fg);
                
//                Set<Viewer> viewerSet = new HashSet<Viewer> ();
//                genreSet.add(genre);
//                viewerSet.add(viewer);
//                genre.setViewers(viewerSet);
//                dg.updatemiddletable(genre);
//                dg.commit();
            }
//            viewer.setGenres(genreSet);
//            dv.updatemiddletable(viewer);
//            dv.update(viewer);
//            dv.commit(); 
             
             
            Set<Actor> actorSet = new HashSet<Actor>();
            //test
//            viewer.getActors().clear();
//            dv.update(viewer);
            
            String[] actorStrings = request.getParameterValues("actors");
            
            for(int i = 0; i < actorStrings.length; i++){
                int actorId = Integer.parseInt(actorStrings[i]);
                               
                DaoActor da = new DaoActor();
             
                Actor actor = (Actor)da.findById(actorId);
                FavouriteActorId fai= new FavouriteActorId(viewer.getIdviewer(), actorId) ;
                FavouriteActor fa = new FavouriteActor(fai);
                actor.getViewers().add(viewer);
                viewer.getActors().add(actor);
                DaoFavouriteActor dfa = new DaoFavouriteActor();
                dfa.save(fa);
                
            }
            dv.update(viewer);
            session.setAttribute("user", viewer);
        
        request.setAttribute("message", "Profile has been edited :<a href='viewer.jsp'>Go back to viewer homepage</a>");
                    return "message";
        
    }
    
    protected Date TransformToDate(String str_date) {
        try {
            DateFormat dfm = new SimpleDateFormat("MM-dd-yyyy");
            return dfm.parse(str_date);
        } catch (ParseException e) {
            System.out.println("Exception :" + e);
        }
        return null;
    }
}
